/*
 * Copyright (C) 2013 Daniel Wasilew <daniel.wasilew@codeminer.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.codeminer.parts.server.users;

import java.util.List;
import java.util.UUID;

/**
 *
 * @author Daniel Wasilew <daniel.wasilew@codeminer.org>
 */
public interface UserManager {

    /**
     * Creates and stores a new user with the supplied name and password.
     * 
     * @param name the username
     * @param passswordHash the user supplied password as sha512hex
     * @return the new created user
     * @throws DuplicatedUserException if the username is already taken
     * @throws UserCreationException if the user could not be created or stored.
     */
    User register(String name, String passswordHash) throws DuplicatedUserException, UserCreationException;

    /**
     * Try to login the supplied user
     * @param name of the user
     * @param passswordHash password as sha512hex
     * @return the user if logged in
     * @throws LoginException if the user could not be logged in with the given credentials
     */
    User login(String name, String passswordHash) throws LoginException;

    /**
     * Returns as sessionId if one is already available.
     * @param user 
     * @return sessionId if one is current available
     * @throws NoSessionException if no session was created for this user
     */
    UUID getSessionId(User user) throws NoSessionException;
    
    /**
     * Returns a User for the supplied SessionId.
     * @param sessionId search for this
     * @return User user for the sessionId
     * @throws NoSessionException if no session was created with this sessionId
     * @throws TimeoutException if a session was created, but is now to old.
     */
    User getUser(UUID sessionId) throws NoSessionException, TimeoutException;
    
    /**
     * Returns the User for the given name.
     * @param name
     * @return User if found, else returns null.
     */
    User getUser(String name) throws LoginException;
    
    /**
     * Logs out the supplied user.
     * All sessioninformation will be discarded.
     * @param user 
     */
    void logout(User user);

    /**
     * Deletes the supplied user.
     * @param user 
     */
    void delete(User user);

    List<User> getAllUsers();
}
